//
//  AreaViewController.m
//  wanhucang
//
//  Created by 郝志宇 on 16/2/22.
//  Copyright © 2016年 XuDong Jin. All rights reserved.
//

#import "AreaViewController.h"
#import "IGGetaddressApi.h"
#import "RegionListModel.h"
#import "CityListModel.h"
#import "CountryListModel.h"
@interface AreaViewController () <UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) NSArray *provis;
@property (strong, nonatomic) NSArray *citys;
@property (strong, nonatomic) NSArray *areas;

@property (nonatomic, strong) RegionListModel *selProvi;
@property (nonatomic, strong) CityListModel *selCity;
@property (nonatomic, strong) CountryListModel *selArea;

@property (strong, nonatomic) void(^block)(RegionListModel *selProvi,  CityListModel *selCity,  CountryListModel *selArea);
@property (nonatomic, strong) IGGetaddressApi *getAddressApi;


@end

@implementation AreaViewController
#pragma mark -生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
//    self.title = @"选择区域";
    self.title = NSLocalizedString(@"Choose Your Area", nil);
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:self action:@selector(back)];
    
    if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    
    self.table1.frame = CGRectMake(0, 0, KSCREENWIDTH/3, KSCREENHEIGHT-64);
    self.table2.frame = CGRectMake(KSCREENWIDTH/3, 0, KSCREENWIDTH/3, KSCREENHEIGHT-64);
    self.table3.frame = CGRectMake(KSCREENWIDTH/3*2, 0, KSCREENWIDTH/3, KSCREENHEIGHT-64);
    
    self.table1.separatorInset = UIEdgeInsetsMake(0, self.table1.width, 0, 0);
    self.table2.separatorInset = UIEdgeInsetsMake(0, self.table2.width, 0, 0);
    self.table3.separatorInset = UIEdgeInsetsMake(0, self.table3.width, 0, 0);
    
    self.table1.backgroundColor = RGB(222, 222, 222);
    self.table2.backgroundColor = RGB(232, 232, 232);
    self.table2.backgroundColor = RGB(242, 242, 242);
    
    [self dataRequest];
}
#pragma mark -懒加载
- (NSArray *)provis {
    if (!_provis) {
        _provis = [NSArray array];
    }
    return _provis;
}

- (NSArray *)citys {
    if (!_citys) {
        _citys = [NSArray array];
    }
    return _citys;
}

- (NSArray *)areas {
    if (!_areas) {
        _areas = [NSArray array];
    }
    return _areas;
}

- (void)dataRequest{
       __weak typeof(self) weakSelf = self;
    self.getAddressApi = [[IGGetaddressApi alloc] init];
    
    [self.getAddressApi startWithDecodeClass:[IGGetaddressResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject) {
            IGGetaddressResp *resp = responseObject;
            IGGetaddressModel *model = resp.data;
            weakSelf.provis = [NSArray arrayWithArray:model.regionList];
            if (weakSelf.provis.count > 0) {
                RegionListModel *regionTmp = weakSelf.provis[0];
                weakSelf.selProvi = regionTmp;
                
                weakSelf.citys = regionTmp.cityList;
                if (weakSelf.citys.count > 0) {
                    CityListModel *cityTmp = weakSelf.citys[0];
                    weakSelf.selCity = cityTmp;
                    
                    weakSelf.areas = cityTmp.countryList;
                    if (self.areas.count > 0) {
                        CountryListModel *countyTmp = weakSelf.areas[0];
                        weakSelf.selArea = countyTmp;
                    }
                }
            }
            [weakSelf.table1 reloadData];
            [weakSelf.table2 reloadData];
            [weakSelf.table3 reloadData];
            }
    }];

}

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == self.table1) {
        return self.provis.count;
    } else if (tableView == self.table2) {
        return self.citys.count;
    } else if (tableView == self.table3) {
        return self.areas.count;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    cell.backgroundColor = tableView.backgroundColor;
    cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    
    if (tableView == self.table1) {
        RegionListModel *provi=self.provis[indexPath.row];
        cell.textLabel.text = provi.provinceName;
    } else if (tableView == self.table2) {
        CityListModel *city = self.citys[indexPath.row];
        cell.textLabel.text = city.cName;
    } else if (tableView == self.table3) {
        CountryListModel *area = self.areas[indexPath.row];
        cell.textLabel.text = area.countryName;
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.table1) {
        RegionListModel *provi = self.provis[indexPath.row];
        _selProvi = provi;

        self.citys = provi.cityList;

        if (self.citys.count > 0) {
            CityListModel *cityTmp = self.citys[0];
            _selCity = cityTmp;
            
            self.areas = cityTmp.countryList;
            if (self.areas.count > 0) {
                CountryListModel *countyTmp = self.areas[0];
                _selArea = countyTmp;
            }
        }
        
        [self.table2 reloadData];
        [self.table3 reloadData];
    } else if (tableView == self.table2) {
        CityListModel *city = self.citys[indexPath.row];
        _selCity = city;
        
        self.areas = city.countryList;
        if (self.areas.count > 0) {
            CountryListModel *countyTmp = self.areas[0];
            _selArea = countyTmp;
        }
        
        self.table3.hidden = NO;
        [self.table3 reloadData];
    } else if (tableView == self.table3) {
        CountryListModel *area = self.areas[indexPath.row];
        _selArea = area;
        
        if (self.block) {
            self.block(_selProvi, _selCity, _selArea);
        }
        
        [self back];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}

- (void)selAreaSucc:(void (^)(RegionListModel *selProvi, CityListModel *selCity, CountryListModel *selArea))block{
    self.block = block;
}

- (void)back {
    [self.navigationController dismissViewControllerAnimated:YES completion:NULL];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
